<!--<template>-->
<!--  <el-container class="deposit-container">-->
<!--    <el-header class="header-style">-->
<!--      <div class="header-content">-->
<!--        <h2>存款管理</h2>-->
<!--        <div class="button-group">-->
<!--          <el-button type="primary" @click="openDialog">新增存款</el-button>-->
<!--          <el-button type="success" @click="downloadExcel">下载Excel</el-button>-->
<!--        </div>-->
<!--      </div>-->
<!--    </el-header>-->
<!--    <el-main class="main-style">-->
<!--      <el-table :data="depositData" border style="width: 100%">-->
<!--        <el-table-column prop="savingID" label="ID"></el-table-column>-->
<!--        <el-table-column prop="savingName" label="存款名称"></el-table-column>-->
<!--        <el-table-column prop="descrip" label="描述"></el-table-column>-->
<!--        <el-table-column label="操作">-->
<!--          <template #default="scope">-->
<!--            <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>-->
<!--            <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--      </el-table>-->
<!--    </el-main>-->
<!--  </el-container>-->

<!--  &lt;!&ndash; 新增/编辑存款对话框 &ndash;&gt;-->
<!--  <el-dialog v-model="dialogVisible" :title="editMode ? '编辑存款' : '新增存款'">-->
<!--    <el-form :model="depositForm">-->
<!--      <el-form-item label="存款名称">-->
<!--        <el-input v-model="depositForm.savingName" placeholder="请输入存款名称"></el-input>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="描述">-->
<!--        <el-input v-model="depositForm.descrip" placeholder="请输入描述"></el-input>-->
<!--      </el-form-item>-->
<!--    </el-form>-->
<!--    <template #footer>-->
<!--      <span class="dialog-footer">-->
<!--        <el-button @click="dialogVisible = false">取消</el-button>-->
<!--        <el-button type="primary" @click="submitDeposit">确认</el-button>-->
<!--      </span>-->
<!--    </template>-->
<!--  </el-dialog>-->
<!--</template>-->

<!--<script>-->
<!--import { ref, onMounted } from 'vue';-->
<!--import axios from 'axios';-->
<!--import * as XLSX from 'xlsx';-->

<!--export default {-->
<!--  name: 'DepositView',-->
<!--  setup() {-->
<!--    const depositData = ref([]);-->
<!--    const dialogVisible = ref(false);-->
<!--    const editMode = ref(false);-->
<!--    const currentRow = ref(null);-->
<!--    const depositForm = ref({-->
<!--      savingName: '',-->
<!--      descrip: '',-->
<!--    });-->

<!--    // 从后端获取存款数据-->
<!--    const fetchDeposits = async () => {-->
<!--      try {-->
<!--        const response = await axios.get('http://localhost:8088/api/deposits');-->
<!--        depositData.value = response.data;-->
<!--      } catch (error) {-->
<!--        console.error('获取存款数据失败:', error);-->
<!--      }-->
<!--    };-->

<!--    // 生命周期钩子：组件挂载后获取存款数据-->
<!--    onMounted(() => {-->
<!--      fetchDeposits();-->
<!--    });-->

<!--    const handleEdit = (index, row) => {-->
<!--      editMode.value = true;-->
<!--      currentRow.value = row;-->
<!--      depositForm.value = { ...row };-->
<!--      dialogVisible.value = true;-->
<!--    };-->

<!--    const handleDelete = (index, row) => {-->
<!--      axios.delete(`http://localhost:8088/api/deposits/delete/${row.savingID}`)-->
<!--          .then(response => {-->
<!--            console.log('删除存款成功:', response.data);-->
<!--            depositData.value = depositData.value.filter(item => item.savingID !== row.savingID);-->
<!--          })-->
<!--          .catch(error => {-->
<!--            console.error('删除存款失败:', error);-->
<!--          });-->
<!--    };-->

<!--    const openDialog = () => {-->
<!--      editMode.value = false;-->
<!--      currentRow.value = null;-->
<!--      depositForm.value = { savingName: '', descrip: '' };-->
<!--      dialogVisible.value = true;-->
<!--    };-->

<!--    const submitDeposit = () => {-->
<!--      const depositInfo = {-->
<!--        savingName: depositForm.value.savingName,-->
<!--        descrip: depositForm.value.descrip-->
<!--      };-->

<!--      if (editMode.value && currentRow.value) {-->
<!--        axios.put(`http://localhost:8088/api/deposits/update/${currentRow.value.savingID}`, depositInfo)-->
<!--            .then(response => {-->
<!--              console.log('更新存款成功:', response.data);-->
<!--              const index = depositData.value.findIndex(item => item.savingID === currentRow.value.savingID);-->
<!--              if (index !== -1) {-->
<!--                depositData.value[index] = { ...depositForm.value, savingID: currentRow.value.savingID };-->
<!--              }-->
<!--            })-->
<!--            .catch(error => {-->
<!--              console.error('更新存款失败:', error);-->
<!--            });-->
<!--      } else {-->
<!--        axios.post('http://localhost:8088/api/deposits/insert', depositInfo)-->
<!--            .then(response => {-->
<!--              console.log('新增存款成功:', response.data);-->
<!--              const newId = response.data.insertedId || depositData.value.length > 0 ? Math.max(...depositData.value.map(item => item.savingID)) + 1 : 1;-->
<!--              depositData.value.push({ savingID: newId, ...depositForm.value });-->
<!--            })-->
<!--            .catch(error => {-->
<!--              console.error('新增存款失败:', error);-->
<!--            });-->
<!--      }-->
<!--      dialogVisible.value = false;-->
<!--    };-->

<!--    // 下载Excel-->
<!--    const downloadExcel = () => {-->
<!--      const workbook = XLSX.utils.book_new();-->
<!--      const worksheet = XLSX.utils.json_to_sheet(depositData.value);-->
<!--      XLSX.utils.book_append_sheet(workbook, worksheet, '存款信息');-->
<!--      XLSX.writeFile(workbook, '存款信息.xlsx');-->
<!--    };-->

<!--    return {-->
<!--      depositData,-->
<!--      dialogVisible,-->
<!--      editMode,-->
<!--      currentRow,-->
<!--      depositForm,-->
<!--      handleEdit,-->
<!--      handleDelete,-->
<!--      openDialog,-->
<!--      submitDeposit,-->
<!--      downloadExcel,-->
<!--    };-->
<!--  },-->
<!--};-->
<!--</script>-->

<!--<style scoped>-->
<!--.deposit-container {-->
<!--  height: 100vh;-->
<!--  display: flex;-->
<!--  flex-direction: column;-->
<!--}-->

<!--.header-style {-->
<!--  background-color: #fff;-->
<!--  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);-->
<!--  padding: 10px 20px;-->
<!--  position: sticky;-->
<!--  top: 0;-->
<!--  z-index: 99;-->
<!--  display: flex;-->
<!--  justify-content: space-between;-->
<!--  align-items: center;-->
<!--}-->

<!--.header-content {-->
<!--  display: flex;-->
<!--  flex: 1;-->
<!--  justify-content: space-between;-->
<!--  align-items: center;-->
<!--}-->

<!--.main-style {-->
<!--  flex: 1;-->
<!--  padding: 20px;-->
<!--  overflow-y: auto;-->
<!--}-->

<!--.el-table {-->
<!--  height: 100%;-->
<!--}-->

<!--.button-group {-->
<!--  display: flex;-->
<!--  gap: 10px;-->
<!--}-->

<!--.dialog-footer {-->
<!--  display: flex;-->
<!--  justify-content: flex-end;-->
<!--}-->
<!--</style>-->

<template>
  <el-container class="deposit-container">
    <el-header class="header-style">
      <div class="header-content">
        <h2>存款管理</h2>
        <div class="button-group">
          <el-button type="primary" @click="openDialog">新增存款</el-button>
          <el-button type="success" @click="downloadExcel">下载Excel</el-button>
        </div>
      </div>
    </el-header>
    <el-main class="main-style">
      <el-table :data="depositData" border style="width: 100%">
        <el-table-column prop="savingID" label="ID"></el-table-column>
        <el-table-column prop="savingName" label="存款名称"></el-table-column>
        <el-table-column prop="descrip" label="描述"></el-table-column>
        <el-table-column prop="cardID" label="银行卡号"></el-table-column>
        <el-table-column prop="amount" label="金额"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
  </el-container>

  <!-- 新增/编辑存款对话框 -->
  <el-dialog v-model="dialogVisible" :title="editMode ? '编辑存款' : '新增存款'">
    <el-form :model="depositForm">
      <el-form-item label="存款名称">
        <el-input v-model="depositForm.savingName" placeholder="请输入存款名称"></el-input>
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="depositForm.descrip" placeholder="请输入描述"></el-input>
      </el-form-item>
      <el-form-item label="银行卡号">
        <el-input v-model="depositForm.cardID" placeholder="请输入银行卡号"></el-input>
      </el-form-item>
      <el-form-item label="金额">
        <el-input-number v-model="depositForm.amount" :min="0.01" :precision="2"></el-input-number>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitDeposit">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { ref, onMounted } from 'vue';
import axios from 'axios';
import * as XLSX from 'xlsx';

export default {
  name: 'DepositView',
  setup() {
    const depositData = ref([]);
    const dialogVisible = ref(false);
    const editMode = ref(false);
    const currentRow = ref(null);
    const depositForm = ref({
      savingName: '',
      descrip: '',
      cardID: '',
      amount: 0,
    });

    // 从后端获取存款数据
    const fetchDeposits = async () => {
      try {
        const response = await axios.get('http://localhost:8088/api/deposits');
        depositData.value = response.data;
      } catch (error) {
        console.error('获取存款数据失败:', error);
      }
    };

    // 生命周期钩子：组件挂载后获取存款数据
    onMounted(() => {
      fetchDeposits();
    });

    const handleEdit = (index, row) => {
      editMode.value = true;
      currentRow.value = row;
      depositForm.value = { ...row };
      dialogVisible.value = true;
    };

    const handleDelete = (index, row) => {
      axios.delete(`http://localhost:8088/api/deposits/delete/${row.savingID}`)
          .then(response => {
            console.log('删除存款成功:', response.data);
            depositData.value = depositData.value.filter(item => item.savingID !== row.savingID);
          })
          .catch(error => {
            console.error('删除存款失败:', error);
          });
    };

    const openDialog = () => {
      editMode.value = false;
      currentRow.value = null;
      depositForm.value = { savingName: '', descrip: '', cardID: '', amount: 0 };
      dialogVisible.value = true;
    };

    const submitDeposit = () => {
      const depositInfo = {
        savingName: depositForm.value.savingName,
        descrip: depositForm.value.descrip,
        cardID: depositForm.value.cardID,
        amount: depositForm.value.amount
      };

      if (editMode.value && currentRow.value) {
        axios.put(`http://localhost:8088/api/deposits/update/${currentRow.value.savingID}`, depositInfo)
            .then(response => {
              console.log('更新存款成功:', response.data);
              const index = depositData.value.findIndex(item => item.savingID === currentRow.value.savingID);
              if (index !== -1) {
                depositData.value[index] = { ...depositForm.value, savingID: currentRow.value.savingID };
              }
            })
            .catch(error => {
              console.error('更新存款失败:', error);
            });
      } else {
        axios.post('http://localhost:8088/api/deposits/insert', depositInfo)
            .then(response => {
              console.log('新增存款成功:', response.data);
              const newId = response.data.insertedId || depositData.value.length > 0 ? Math.max(...depositData.value.map(item => item.savingID)) + 1 : 1;
              depositData.value.push({ savingID: newId, ...depositForm.value });
            })
            .catch(error => {
              console.error('新增存款失败:', error);
            });
      }
      dialogVisible.value = false;
    };

    // 下载Excel
    const downloadExcel = () => {
      const workbook = XLSX.utils.book_new();
      const worksheet = XLSX.utils.json_to_sheet(depositData.value);
      XLSX.utils.book_append_sheet(workbook, worksheet, '存款信息');
      XLSX.writeFile(workbook, '存款信息.xlsx');
    };

    return {
      depositData,
      dialogVisible,
      editMode,
      currentRow,
      depositForm,
      handleEdit,
      handleDelete,
      openDialog,
      submitDeposit,
      downloadExcel,
    };
  },
};
</script>

<style scoped>
.deposit-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.header-style {
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 10px 20px;
  position: sticky;
  top: 0;
  z-index: 99;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-content {
  display: flex;
  flex: 1;
  justify-content: space-between;
  align-items: center;
}

.main-style {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
}

.el-table {
  height: 100%;
}

.button-group {
  display: flex;
  gap: 10px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>